package com.chengwei.ch.busdemo.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.chengwei.ch.busdemo.base.BaseActivity;
import com.chengwei.ch.busdemo.view.CustomVideoView;
import com.chengwei.ch.busdemo.R;
import com.gyf.barlibrary.ImmersionBar;
import com.zyl.customkeyboardview.CustomKeyboardEditText;

import static com.chengwei.ch.busdemo.APP.mScreensaver;

public class VideoActivity extends BaseActivity {

    //创建播放视频的控件对象
    private int videoPath;
    private CustomVideoView mVideoView;
    private ConstraintLayout clContainerVideo;
    private Button btnScanVideo, btnBuyVideo;
    private int nowtime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initImmersionBar();
        initView();
        initClickListener();
        initVideo();
    }

    @SuppressLint("ObsoleteSdkInt")
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initImmersionBar() {
        ImmersionBar
                .with(this)
                .keyboardEnable(true)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarColor(android.R.color.transparent)
                .init();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(Color.parseColor("#000000"));
        }
    }


    private void initView() {
        btnScanVideo = findViewById(R.id.btnScanVideo);
        btnBuyVideo = findViewById(R.id.btnBuyVideo);
        clContainerVideo = findViewById(R.id.clContainerVideo);
        mVideoView = findViewById(R.id.videoView);
        //摇摆  
        /**
         *   float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值；
         *
         * 　float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值；
         *
         * 　float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值；
         *
         * 　float toYDelta这个参数表示动画开始的点离当前View Y坐标上的差值；
         */
        TranslateAnimation alphaAnimation = new TranslateAnimation(0f, 0f, 0f, -60f);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        clContainerVideo.setAnimation(alphaAnimation);
        alphaAnimation.start();
    }


    private void initClickListener() {
        btnScanVideo.setOnClickListener(this);
        btnBuyVideo.setOnClickListener(this);
    }

    private void initVideo() {
        //设置将要播放视频文件的加载路径仅支持 3gp、MP4、avi\
        videoPath = R.raw.play1;
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videoPath));
        //播放
        mVideoView.start();
        //播放准备完毕
        mVideoView.setOnPreparedListener(mp -> {
            mp.setVolume(0f, 0f);
            // mVideoView.start();
        });
        //循环播放
        mVideoView.setOnCompletionListener(mp -> {
            switch (videoPath) {
                case R.raw.play1: {
                    videoPath = R.raw.play1;
                    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videoPath));
                    break;
                }
            /*    case R.raw.play2: {
                    videoPath = R.raw.play3;
                    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videoPath));
                    break;
                }
                case R.raw.play3: {
                    videoPath = R.raw.play4;
                    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videoPath));
                    break;
                }
                case R.raw.play4: {
                    videoPath = R.raw.play1;
                    mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videoPath));
                    break;
                }*/
                default:
                    break;
            }
            mVideoView.start();
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btnScanVideo: {
                showDialog();
                break;
            }
            case R.id.btnBuyVideo: {
                intent.setClass(this, BuyTicketActivity.class);
                startActivity(intent);
                mScreensaver.resetTime();
                break;
            }
            default:
                break;
        }
    }

    CustomKeyboardEditText et = null;

    private void showDialog() {
        Dialog dialog = new Dialog(VideoActivity.this);
        dialog.setContentView(R.layout.dialog_input_card);
        CustomKeyboardEditText etNameIdCard = dialog.findViewById(R.id.etNameIdCardDis);
        Button btnConfirm = dialog.findViewById(R.id.btnConfirmIdCardDis);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        window.setBackgroundDrawable(new ColorDrawable());
        dialog.show();
        new Handler().postDelayed(() -> {
            etNameIdCard.showKeyboard();
        }, 100);
        btnConfirm.setOnClickListener(v -> {
            String idCard = etNameIdCard.getText().toString();
            ToastUtils.showShort(idCard);
            dialog.dismiss();
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mScreensaver.stop();
        if (mVideoView != null) {
            if (!mVideoView.isPlaying()) {
                mVideoView.seekTo(nowtime);
                mVideoView.start();
            }
        }
    }

    //返回重启加载
    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        nowtime = mVideoView.getCurrentPosition();  //记录当前帧位置int
    }

    //防止锁屏或者切出的时候，音乐在播放
    @Override
    protected void onStop() {
        super.onStop();
        mVideoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        mScreensaver.resetTime();
        mVideoView.stopPlayback();
    }

}
